package com.sgcc.ln.pmos.e_ln_mall.adapter;

import static com.sgcc.ln.pmos.e_ln_mall.adapter.GoodsAdapter.getMaxMonth;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sgcc.ln.pmos.common.bean.PackageBean;
import com.sgcc.ln.pmos.common.http.HttpInterConst;
import com.sgcc.ln.pmos.common.utils.BigDecimalUtils;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.ln.pmos.common.utils.EnumUtils;
import com.sgcc.ln.pmos.common.utils.GlideUtil;
import com.sgcc.ln.pmos.common.utils.PackageContrastUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.common.utils.TextUtil;

public class GoodsTwoLinAdapter extends BaseQuickAdapter<PackageBean, BaseViewHolder> {

    private Context mContext;

    public GoodsTwoLinAdapter(Context context) {
        super(R.layout.item_goods3);
        mContext = context;
    }

    public interface OnItemClickListener {
        //加入收藏
        void OnAddCollectionListener(int position, PackageBean s);

        //取消收藏
        void OnCancleCollectionListener(int position, PackageBean s);

        //条目点击
        void OnItemsClickListener(int position, PackageBean s);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, PackageBean bean) {

        String img_url = HttpInterConst.BASEURL_IMG + bean.getFileId();
        ImageView imageView = baseViewHolder.getView(R.id.iv_goods);
//        Glide.with(mContext).load(img_url).placeholder(R.mipmap.deft_goods).error(R.mipmap.deft_goods).into(imageView);
        GlideUtil.loadImg(img_url, imageView,R.mipmap.deft_goods);
//        baseViewHolder.setText(R.id.tv_combName, bean.getSellingPoint());
        baseViewHolder.setText(R.id.tv_combName, TextUtil.getName(bean.getSellingPoint(),bean.getCreditRating()));
//        baseViewHolder.setText(R.id.tv_price, BigDecimalUtils.onKeep2Decimal(bean.getPrice()));
        baseViewHolder.setText(R.id.tv_price, bean.getPrice());
        baseViewHolder.setText(R.id.tv_time, DateUtils.getStrng_to_Times_ymd_1(bean.getEffectiveDate()) + "~" + DateUtils.getStrng_to_Times_ymd_1(bean.getInvalidDate()));



        String str = "";
        int maxMonth = getMaxMonth(bean);
        if ("04".equals(bean.getConstrMinCyc())) {
            str = maxMonth + "~" + maxMonth + "自然月";
        } else {
            if (TextUtils.isEmpty(bean.getConstrMin())) {
                if (TextUtils.isEmpty(bean.getConstrMax())) {
                    str = maxMonth + EnumUtils.getConstrMin(bean.getConstrMinCyc());
                } else {
                    str = bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc());
                }
            } else {
                if (TextUtils.isEmpty(bean.getConstrMax())) {
                    str = bean.getConstrMin() + "~" + maxMonth + EnumUtils.getConstrMin(bean.getConstrMinCyc());
                } else {
                    str = bean.getConstrMin() + "~" +bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc());
                }
//                str = bean.getConstrMin() + "~" + bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc());
            }
        }
        if(bean.getConstrMinCyc().equals("04")){
            if(maxMonth==0){
                baseViewHolder.setText(R.id.tv_constrMinCyc,  "全部可购买月份");
            }else{
                baseViewHolder.setText(R.id.tv_constrMinCyc,  str);
            }
        }else{
            baseViewHolder.setText(R.id.tv_constrMinCyc,  str);
        }
//        baseViewHolder.setText(R.id.tv_constrMinCyc, bean.getConstrMinCyc().equals("04") ? "全部可购买月份" : bean.getConstrMin() + "~" + bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc()));

        if (!TextUtils.isEmpty(bean.getPrice())) {
            if (bean.getPrice().contains("~")) {
                baseViewHolder.setGone(R.id.tv_danwei_heng, true);
                baseViewHolder.setGone(R.id.tv_danwei_shu, false);
            }else{
                baseViewHolder.setGone(R.id.tv_danwei_heng, false);
                baseViewHolder.setGone(R.id.tv_danwei_shu, true);
            }
        }else{
            baseViewHolder.setGone(R.id.tv_danwei_heng, true);
            baseViewHolder.setGone(R.id.tv_danwei_shu, true);
        }


        if ("1".equals(bean.getpNum()) && ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) &&
                "0".equals(bean.getMainItemType()) &&
                ("0".equals(bean.getMainDifType()) || TextUtils.isEmpty(bean.getMainDifType()))
        ) {//单一套餐&&不分时段&&固定价格&&没有浮动价差
            //蓝色
            baseViewHolder.setTextColor(R.id.tv_price, Color.parseColor("#0092FF"));
            baseViewHolder.setGone(R.id.tv_ygjg, true);
//            baseViewHolder.setGone(R.id.tv_listyudianjia, true);
        } else {
            //橙色
            baseViewHolder.setTextColor(R.id.tv_price, Color.parseColor("#FFA500"));
            baseViewHolder.setGone(R.id.tv_ygjg, false);
//            baseViewHolder.setGone(R.id.tv_listyudianjia, false);
        }

        if ("00".equals(bean.getUstatus())) {////收藏状态： 00未收藏 01 已收藏
            //未收藏
            baseViewHolder.setImageResource(R.id.iv_collection, R.mipmap.mall_collection);
            baseViewHolder.setText(R.id.tv_collection, "加入收藏");
            baseViewHolder.getView(R.id.rl_collection).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.OnAddCollectionListener(getItemPosition(bean), bean);
                    }
                }
            });
        } else {
            baseViewHolder.setImageResource(R.id.iv_collection, R.mipmap.mall_collectioned);
            baseViewHolder.setText(R.id.tv_collection, "已收藏");
            baseViewHolder.getView(R.id.rl_collection).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.OnCancleCollectionListener(getItemPosition(bean), bean);
                    }
                }
            });
        }

        baseViewHolder.setText(R.id.tv_green, EnumUtils.getGreenLeft(bean.getConstrRel()) + "  " + EnumUtils.getGreenCenter(bean.getConstrMod()) + "  " + EnumUtils.getGreenRight(bean.getConstrBarg()));

        baseViewHolder.getView(R.id.rl_store).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnItemsClickListener(getItemPosition(bean), bean);
                }
            }
        });

        if (PackageContrastUtils.exists(bean.getCombId())) {
            baseViewHolder.setImageResource(R.id.iv_add_pk, R.mipmap.mall_add_pked);
            baseViewHolder.setText(R.id.tv_add_pk, "已加入");
            baseViewHolder.getView(R.id.rl_add_pk).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    PackageContrastUtils.deletContrast(bean.getCombId());
                    notifyDataSetChanged();
                }
            });
        } else {
            baseViewHolder.setImageResource(R.id.iv_add_pk, R.mipmap.mall_add_pk);
            baseViewHolder.setText(R.id.tv_add_pk, "加入对比");
            baseViewHolder.getView(R.id.rl_add_pk).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    PackageContrastUtils.addContrast(bean);
                    notifyDataSetChanged();
                }
            });
        }
    }


}
